package morenoapp.components.config2;

import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import morenoapp.action.MorenoActionsFacade;
import morenoapp.MorenoApp;
import morenoapp.action.resources.ActionsResourcesHolder;
import morenoapp.components.common.HintTextArea;
import morenoapp.components.common.HintTextField;
import morenoapp.components.help.MorenoHelp;
import morenoapp.model.data.MorenoModelData;
import morenoapp.model.data.MorenoModelFace;
import morenoapp.model.enums.MorenoPollType;
import morenoapp.resources.ResourcesHolder;
import morenoapp.util.DocumentedComponent;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;
import org.jdesktop.application.Action;

/**
 *
 * @author root
 */
@DocumentedComponent
public class Configuration2JPanel extends JPanel {
    MorenoModelFace model = MorenoApp.getModel();
    HintTextField textResearchName;
    JTextArea textResearchTarget;
    HintTextField textResearchGroup;
    HintTextField textSupervisor;
    HintTextField textResearchPollDate;
    JRadioButton radioNonParametricPoll;
    JRadioButton radioParametricPoll;
    JTextArea textInquirerIntro;
    JButton buttonInquirerAddQuestion;
    JButton buttonInquirerPrint;
    JButton buttonNext;
    QuestionsListEditorJPanel panelQuestionsListEditor;
    
    public Configuration2JPanel() {
        initComponents();
        initLayout();
    }

    private void initComponents() {
        {
            HintTextField text = new HintTextField();
            text.setName("textResearchName");
            text.setHint(I18N.get("config.research_name_hint"));
            { //binding
                ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_RESEARCHNAME, true);
                Bindings.bind(text, value);
            }
            textResearchName = text;
        }
        {
            JTextArea text = new JTextArea();
            text.setName("textResearchTarget");
            { //binding
                ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_RESEARCHTARGET, true);
                Bindings.bind(text, value);
            }
            textResearchTarget = text;
        }
        {
            HintTextField text = new HintTextField();
            text.setName("textResearchGroup");
            text.setHint(I18N.get("config.research_group_hint"));
            { //binding
                ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_RESEARCHGROUP, true);
                Bindings.bind(text, value);
            }
            textResearchGroup = text;
        }
        {
            HintTextField text = new HintTextField();
            text.setName("textSupervisor");
            text.setHint(I18N.get("config.supervisor_hint"));
            { //binding
                ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_SUPERVISORINFO, true);
                Bindings.bind(text, value);
            }
            textSupervisor = text;
        }
        {
            HintTextField text = new HintTextField();
            text.setName("textResearchPollDate");
            text.setHint(I18N.get("config.research_poll_date_hint"));
            { //binding
                ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_POLLDATE, true);
                Bindings.bind(text, value);
            }
            textResearchPollDate = text;
        }
        { //poll type radiobuttons
            {
                JRadioButton radio = new JRadioButton();
                radio.setName("radioNonParametricPoll");
                radio.setText(I18N.get("config.research_type_nonparametric"));
                { //binding
                    ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_POLLTYPE, true);
                    Bindings.bind(radio, value, MorenoPollType.NonParametricPoll);
                }
                radioNonParametricPoll = radio;
            }
            {
                JRadioButton radio = new JRadioButton();
                radio.setName("radioParametricPoll");
                radio.setText(I18N.get("config.research_type_parametric"));
                { //binding
                    ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_POLLTYPE, true);
                    Bindings.bind(radio, value, MorenoPollType.ParametricPoll);
                }
                radioParametricPoll = radio;
            }
        }
        {
            HintTextArea text = new HintTextArea();
            text.setName("textInquirerIntro");
            text.setHint(I18N.get("config.inquirer_intro_hint"));
            { //binding
                ValueModel value = new PropertyAdapter(model, MorenoModelData.PROPERTY_INQUIRERINTRO, true);
                Bindings.bind(text, value);
            }
            textInquirerIntro = text;
        }
        {
            JButton button = new JButton();
            button.setName("buttonInquirerAddQuestion");
            button.setIcon(ActionsResourcesHolder.ICON_ACTION_ADD_SMALL);
            button.setText(I18N.get("config.add_question"));
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    actionAddQuestion();
                }
            });
            buttonInquirerAddQuestion = button;
        }
        {
            JButton button = new JButton();
            button.setName("buttonInquirerPrint");
            button.setIcon(ResourcesHolder.iconPrintSmall);
            button.setText(I18N.get("config.print_inquirer"));
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    actionPrintQuestionsList();
                }
            });
            buttonInquirerPrint = button;
        }
        {
            panelQuestionsListEditor = new QuestionsListEditorJPanel();
        }
        {
            JButton button = new JButton();
            button.setName("buttonNext");
            button.setIcon(ResourcesHolder.iconNavigateNextSmall);
            button.setText(I18N.get("config.go_next_step"));
            button.setHorizontalTextPosition(SwingConstants.LEFT);
            button.addActionListener(MorenoActionsFacade.getActionNavigateNext());
            buttonNext = button;
        }
    }

    private void initLayout() {
        FormLayout layout = new FormLayout(
                //label: space field space field help
                "right:pref, 3dlu, 20px:grow(70), 3dlu, 20px:grow(30), pref",
                "p, 3dlu, " + //separator
                "p, 3dlu, " + //research name
                //TODO add psychologist name
                "70px, 3dlu, " + //goals
                "p, 3dlu, " + //target group
                "p, 3dlu, " + //date
                "p, 3dlu, " + //poll type
                "p, 3dlu, " + //separator
                "100px, 3dlu, " + //inquirer
                "p, 3dlu, " + //inquirer buttons bar
                "p, 3dlu, " + //questions list
                "p, " + //button next
                ""); // rows

        //PanelBuilder builder = new PanelBuilder(layout, new FormDebugPanel()); //debug
        PanelBuilder builder = new PanelBuilder(layout);
        //builder.setDefaultDialogBorder();
        builder.setBorder(new javax.swing.border.EmptyBorder(5, 5, 5, 5));

        // Obtain a reusable constraints object to place components in the grid.
        CellConstraints cc = new CellConstraints();

        // Fill the grid with components
        final int defaultColumnSpan = -2 + layout.getColumnCount(); //label2 space-body-space help2
        final int labelColumn = 1;
        final int fieldColumn = 3;
        final int helpColumn = layout.getColumnCount();
        int row = 1;
        builder.addSeparator(I18N.get("config.research_description"), cc.rcw(row, labelColumn, layout.getColumnCount()-1));
        builder.add(MorenoHelp.createHelpToolbar("research_info"), cc.rc(row, layout.getColumnCount()));
        row += 2; //Next Row
        builder.addLabel(I18N.get("config.rsearch_name"), cc.rc(row, labelColumn));
        builder.add(textResearchName, cc.rcw(row, fieldColumn, defaultColumnSpan));
        row += 2; //Next Row
        builder.addLabel(I18N.get("config.research_target"), cc.rc(row, labelColumn, "top, right"));
        builder.add(new JScrollPane(textResearchTarget), cc.rcw(row, fieldColumn, defaultColumnSpan, "fill, fill"));
        row += 2; //Next Row
        builder.addLabel(I18N.get("config.research_group"), cc.rc(row, labelColumn));
        builder.add(textResearchGroup, cc.rcw(row, fieldColumn, defaultColumnSpan));
        { //psycholog name and poll date
            row += 2; //Next Row
            builder.addLabel(I18N.get("config.researcher"), cc.rc(row, labelColumn));
            JPanel panelBox = MorenoUtil.createBoxLayoutPanel(BoxLayout.LINE_AXIS);
            panelBox.add(MorenoUtil.prefWidth(textSupervisor, 100));
            panelBox.add(Box.createHorizontalStrut(10));
            panelBox.add(new JLabel(I18N.get("config.research_poll_date")));
            panelBox.add(MorenoUtil.prefWidth(textResearchPollDate, 50));
            panelBox.add(Box.createGlue());
            panelBox.add(Box.createHorizontalStrut(0));
            builder.add(panelBox, cc.rcw(row, 3, layout.getColumnCount()-2, "default, default"));
        }
        { //research type
            row += 2; 
            builder.addLabel(I18N.get("config.research_type"), cc.rc(row, labelColumn));
            JPanel panelBox = new JPanel();
            BoxLayout layoutBox = new BoxLayout(panelBox, BoxLayout.LINE_AXIS);
            panelBox.setLayout(layoutBox);
            panelBox.add(radioNonParametricPoll);
            panelBox.add(Box.createHorizontalStrut(20));
            panelBox.add(radioParametricPoll);
            panelBox.add(Box.createHorizontalStrut(10));
            panelBox.add(MorenoHelp.createHelpToolbar("research_poll_types"));
            builder.add(panelBox, cc.rcw(row, 3, -3+layout.getColumnCount()));
        }
        row += 2; //section: inquier
        builder.addSeparator(I18N.get("config.questions_list"), cc.rcw(row, 1, layout.getColumnCount()-1));
        builder.add(MorenoHelp.createHelpToolbar("inquirer"), cc.rc(row, layout.getColumnCount()));
        row += 2; //Goto: inquirer into
        builder.addLabel(I18N.get("config.inquirer_intro"), cc.rc(row, 1, "top, right"));
        builder.add(new JScrollPane(textInquirerIntro), cc.rcw(row, 3, -2 + layout.getColumnCount(), "fill, fill"));
        { //inquirer buttons bar
            row += 2; 
            JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
            toolbar.setFloatable(false);
            toolbar.setBorderPainted(false);
            toolbar.add(buttonInquirerAddQuestion);
            //toolbar.addSeparator();
            toolbar.add(buttonInquirerPrint);
            builder.add(toolbar, cc.rcw(row, 1, layout.getColumnCount(), "default, fill"));
        }
        {
            row += 2; //Goto: qiestons list
            builder.add(panelQuestionsListEditor, cc.rcw(row, 1, layout.getColumnCount(), "fill, fill"));
        }
        row += 2; //Goto: button next
        builder.add(buttonNext, cc.rcw(row, 1, layout.getColumnCount(), "default, right"));

        { //place nested components into panel
            setLayout(new BorderLayout(0, 0));
            JScrollPane scroll = new JScrollPane(builder.getPanel());
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            add(scroll, BorderLayout.CENTER);
        }
    }

    @Action
    public void actionAddQuestion() {
        MorenoApp.getApplicationData().getMorenoModelProxy().createQuestion();
    }

    @Action
    public void actionPrintQuestionsList() {
        MorenoApp.showSorryDialog(buttonInquirerPrint);
    }
}
